let human = [
  { name: "zhangsan", age: 10 },
  { name: "lisi", age: 20 },
  { name: "wangwu", age: 12 },
  { name: "zhaoliu", age: 30 },
];
//  human 每个元素打印一下 方便测试
human.forEach((e) => {
  console.log(e);
});
// human 舍去未成年人
let humans = human.filter((e) => {
  // console.log(e);
  return e.age > 18;
});
//  human 过滤掉舍弃名字字符长度小于5的人
let humanss = human.filter((e) => {
  return !(e.name.length < 5);
});
// human中的每一个人的名字变为大写
let humanes = human.map((x) => {
  return {
    username: x.name.toUpperCase(),
    age: x.age,
    sex: true,
  };
});
let states = [
  { value: "saved", label: "已暂存", index: 0 },
  { value: "submitted", label: "已提交", index: 1 },
  { value: "audited", label: "已审核", index: 2 },
];
let leaveBill = {
  name: "张三",
  leaveReason: "xxxxx",
  leaveState: "submitted",
};
// "已提交" 根据表单中的状态 查找出中文信息 显示给用户
let r = "";
let rr = states.filter((x) => {
  return x.value === leaveBill.leaveState;
});
r = rr[0].label;
console.log(r);
let leaveBills = [
  {
    name: "张三",
    leaveReason: "xxxxx",
    leaveState: "submitted",
  },
  {
    name: "李四",
    leaveReason: "xxxxx",
    leaveState: "audited",
  },
  {
    name: "王五",
    leaveReason: "xxxxx",
    leaveState: "saved",
  },
];
// 输出一个新的NewleaveBills 其中每个 新的NewleaveBill 俩属性
// {
//     username:"张三",
//     leaveState:"已提交"
// }
// [{username:"张三",leaveState:"已提交"},
//  {username:"李四",leaveState:"已审核"},
//  {username:"王五",leaveState:"已暂存"},]
let rrr = leaveBills.map((bill) => {
  // bill.leaveState saved
  // bill.leaveState
  let ss = states.filter((state) => {
    // state. bill
    return state.value === bill.leaveState;
  });

  return {
    username: bill.name,
    leaveState: ss[0].label,
  };
});

// let rrr = leaveBills.map((bill) => {
//   let ss = states.filter(state=>state.value ===bill.leaveState)
//   return {
//     username: bill.name,
//     leaveState: ss[0].label,
//   };
// });

states = [
  { value: "saved", label: "已暂存", index: 2 },
  { value: "submitted", label: "已提交", index: 13 },
  { value: "audited", label: "已审核", index: 23 },
];
let target = "submitd";
let result = "";
let ee = states.filter((x) => {
  return x.value === target;
});
let rs = ee[0] ? ee[0].label : "";
// let rs ="";
// if(ee.length > 0) {
//   rs =ee[0].label
// }

console.log(rs);
let pp = states.find((x) => {
  return x.value === target;
});
// 查无此人 返回值 undefined
let rsr = pp ? pp.label : "";

let t = states.findIndex((x) => {
  return x.value === target;
});
// 查无此人 返回值 -1
console.log(t);
// console.log()
// filter(find findIndex) map reduce
// sort
// 数组对象内存模型
// 扩展运算符。
// reduce 带初始值 不带初始值
states = [
  { value: "saved", label: "已暂存", index: 2 },
  { value: "submitted", label: "已提交", index: 13 },
  { value: "audited", label: "已审核", index: 23 },
];
//  0    2   15  38
//  带初始值 时 循环次数 = 数组长度
//  prev 是上一次循环的返回值，代表了累计的总量
//  curr代表当前的数组内元素 也即是数组内的每一个元素
//  返回值是prev 当前累计的总量
let s = states.reduce((prev, curr) => {
  console.log(prev, curr);
  let sss = prev + curr.index;
  console.log("返回：" + sss);
  return sss;
}, 0);
console.log(s);
let goods = [
  { label: "葡萄", price: 2.2, quantity: 10 },
  { label: "橘子", price: 4.5, quantity: 20 },
  { label: "香蕉", price: 12.0, quantity: 2 },
];
let zzz = goods.reduce((prev, curr) => {
  return prev + curr.price * curr.quantity;
}, 0);
console.log(zzz.toFixed(2));

// let ss= [1,2,3]
// let p = ss.reduce((prev, curr) => {
//   console.log("----"+curr);
//   return prev+curr
// })
// console.log(p)
let goodsex = [
  { label: "葡萄", price: 2.2, quantity: 10 },
  { label: "橘子", price: 4.5, quantity: 20 },
  { label: "葡萄", price: 12.0, quantity: 2 },
  { label: "橘子", price: 4.5, quantity: 20 },
  { label: "葡萄果汁", price: 12.0, quantity: 2 },
];
// 以label相同为判定条件去重
// [ { label: "葡萄", price: 12.0, quantity: 2 },
//  { label: "橘子", price: 4.5, quantity: 20 },
//  { label: "葡萄果汁", price: 12.0, quantity: 2 },]
//  [{ label: "葡萄", price: 2.2, quantity: 10 }.,
// { label: "橘子", price: 4.5, quantity: 20 },]
let zz = [];
let sss = goodsex.reduce((prev, curr) => {
  let s = prev.find((x) => x.label === curr.label);
  if (!s) {
    prev.push(curr);
  }
  return prev;
}, zz);
console.log(zz);
console.log(sss);
goods = [
  { label: "葡萄", price: 2.2, quantity: 10 },
  { label: "橘子", price: 4.5, quantity: 20 },
  { label: "香蕉", price: 12.0, quantity: 2 },
];
// sort 升序 负重不移动
let rrrsss = goods.sort((a, b) => b.quantity - a.quantity);
console.log(rrrsss);

let A = { label: "葡萄", price: 2.2, quantity: 10 };
let B = { label: "橘子", price: 4.5, quantity: 20 };

let G = [A, B];
let H = [A, ];
// goods = [
//   { label: "葡萄", price: 2.2, quantity: 10 },
//   ,
//   { label: "香蕉", price: 12.0, quantity: 2 },
// ];
//  other =[
// { label: "葡萄", price: 2.2, quantity: 10 },
//   { label: "橘子", price: 4.5, quantity: 20 },
//  ]
//  ["zhangsaan","lisi","wangwu"]
//  对象复制 
// ...作用在容器类对象上，能够去除字面量上的一层括号
// 扩展运算符 散开
let tt = [1,true,"zhang",{age:10},[2,3]];
// 使用扩展运算符 散开数组再包装成数组 能够复制数组（浅复制）
let ss = [...tt]
// 如果数组或对象内的元素值均为基本元素 建议使用浅复制 即可完成。

// 深复制 递归代码 JSON序列化反序列化
let uu = JSON.parse(JSON.stringify(tt));

// 
// 简单类型基本类型 栈内存复制
let b = function(e){
  e++;
  console.log(e);
}
let a = 22;
b(a);
console.log(a);

// 复杂类型引用类型 没有复制 只有引用
let bbb = function(e){
  e.value++;
  console.log(e);
}
let aaa = {value:22};
bbb(aaa);
console.log(aaa);

